package com.exam.auth.service;

import com.exam.auth.client.UserClient;
import com.exam.auth.config.JwtProperties;
import com.exam.auth.entiy.UserInfo;
import com.exam.auth.utils.JwtUtils;
import com.exam.common.entity.Request;
import com.exam.user.pojo.User;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

/**
 * @ClassName : AuthService
 * @Description : 授权中心
 * @Author : WHB
 */

@Service
@EnableConfigurationProperties(JwtProperties.class)
public class AuthService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties prop;

    public String login(String username, String password){

            try {
                //校验用户名和密码
                User user = userClient.queryUser(username, password);
                if (user == null){
                    System.out.println("用户账号密码错误");
                }
                //生成token
                String token = JwtUtils.generateToken(new UserInfo(user.getId(), username,user.getRole()), prop.getPrivateKey(), prop.getExpire());

                return token;
            }catch (Exception e){
                e.printStackTrace();
            }
        return null;
    }

}

